package com.app.controller;

import java.io.File;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import ch.qos.logback.core.pattern.color.ANSIConstants;

import com.app.entity.*;
import com.app.service.*;

@Controller
@RequestMapping("/answer")
public class AnswerController {
    @Resource
    AnswerService answerService;

    @RequestMapping("/answer/add")
    public String answerAdd() {
        return "admin/answer/add";
    }

    @RequestMapping(value = "/answer/save", method = RequestMethod.POST)
    public String answerSave(String support,
                             String uid,
                             String qid,
                             String answer,
                             String created,
                             String status,
                             String is_true,
                             Model model) {
        Answer modelX = new Answer();
        modelX.setSupport(support);
        modelX.setUid(uid);
        modelX.setQid(qid);
        modelX.setAnswer(answer);
        modelX.setCreated(created);
        modelX.setStatus(status);
        modelX.setIs_true(is_true);

        answerService.insert(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("message", "Success!");
        return "admin/answer/add";
    }

    @RequestMapping("/answer/delete")
    public String answerDelete(int id) {
        answerService.delete(id);
        return "redirect:list";
    }
    @Resource
    QuestionService questionService;
    @Resource
    UsersService usersService;
    @RequestMapping("/answer/list")
    public ModelAndView answerList() {
        List<Answer> answerList = answerService.getAllAnswer();
        
        for (Answer answer : answerList) {
			answer.setUser(usersService.getUsersById(Integer.parseInt(answer.getUid())).getUsername());
			answer.setQuestion(questionService.getQuestionById(Integer.parseInt(answer.getQid())).getShort_title());
		}
        System.out.println(answerList.size());
        ModelAndView mav = new ModelAndView("admin/answer/list");
        mav.addObject("list", answerList);
        return mav;
    }

    @RequestMapping("/answer/edit")
    public String answerEdit(String id, Model model) {
        Answer answer = answerService.getAnswerById(Integer.parseInt(id));
        model.addAttribute("model", answer);
        return "admin/answer/edit";
    }

    @RequestMapping(value = "/answer/update", method = RequestMethod.POST)
    public String answerUpdate(String support,
                               String uid,
                               String qid,
                               String answer,
                               String created,
                               String status,
                               String is_true,
                               int id,
                               Model model) {

        Answer modelX = answerService.getAnswerById(id);
        modelX.setSupport(support);
        modelX.setUid(uid);
        modelX.setQid(qid);
        modelX.setAnswer(answer);
        modelX.setCreated(created);
        modelX.setStatus(status);
        modelX.setIs_true(is_true);

        answerService.update(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("model", modelX);
        model.addAttribute("message", "Success");
        return "admin/answer/edit";
    }
}
